<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      title="新增员工"
      :visible="showadd"
      @close="cancel"
    >
      <el-form ref="myform" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment">
            <el-option
              v-for="(item, index) in hireType"
              :key="index"
              :label="item.value"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div class="box">
            <!-- 输入框 -->
            <el-input @focus="myfocus" v-model="form.departmentName"></el-input>
            <!-- 树状结构 -->
            <el-tree
            v-if="showTree" 
            :data="list"
            :props="{label:'name'}"
            class="mytree"
            default-expand-all
            :expand-on-click-node="false"
            @node-click="nodeclick"
            >
            </el-tree>
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="submit" type="primary">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import employeesEnus from '@/api/constant/employees'
import {getDepartmentsApi} from '@/api/departments' 
import {addEmployeesAllListApi} from '@/api/employees'
export default {
  props: ['showadd'],
  data () {
    return {
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '赶紧填', trigger: 'blur' }],
        mobile: [
          { required: true, message: '赶紧填', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/

              reg.test(value) ? callback() : callback(new Error('错误'))
            },
            trigger: 'blur'
          }
        ],
        timeOfEntry: [{ required: true, message: '赶紧填', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '赶紧填', trigger: 'blur' }
        ],
        workNumber: [{ required: true, message: '赶紧填', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '赶紧填', trigger: 'blur' }
        ],
        correctionTime: [{ required: true, message: '赶紧填', trigger: 'blur' }]
      },
      list:[],
      hireType: employeesEnus.hireType,
      showTree:false
    }
  },
  methods: {
      submit(){
          this.$refs.myform.validate(async isOK=>{
              if(isOK){
                  await addEmployeesAllListApi(this.form)
                  this.$emit('update:showadd',false )
                  this.form={
                     username: '',
                     mobile: '',
                     timeOfEntry: '',
                     formOfEmployment: '',
                     workNumber: '',
                     departmentName: '',
                     correctionTime: ''
                  }
                    // 清除表单的校验
                    this.$refs.myform.resetFields()
                  this.$message.success('新增成功')
                  this.$emit('updateData', )
              }
          })
      },
      //获取部门信息
    async getDepartment(){
          const res=await getDepartmentsApi()
          this.list=this.trunData(res.data.depts,'')
    },
    trunData(arr,pid){
        const newArr=[]
        arr.forEach(item=> {
            if(item.pid===pid){
                const children=this.trunData(arr,item.id)
                if(children.length>0){
                    item.children=children
                }
                newArr.push(item)
            }
        })
        return newArr
    },
      //取消按钮
    cancel () {    
      this.$emit('update:showadd', false)
    },
    //点击部门获取数据
    nodeclick(data){
        this.showTree=false
        this.form.departmentName=data.name
    },
    //表单失去焦点
    myfocus(){
        this.showTree=true
    }
  },
  created() {
      this.getDepartment()
  },
}
</script>

<style lang="scss">
  .box {
    position: relative;
    width: 300px;
    .mytree {
      position: absolute;
      border: 1px solid #ddd;
      border-radius: 5px;
      width: 100%;
      top: 44px;
      left: 0;
      z-index: 999;
      .el-tree-node__content {
        height: 30px;
        .el-icon-caret-right {
          display: none;
        }
        .el-tree-node__label {
          margin-left: 10px;
        }
      }
    }
  }
</style>
